package com.agritech.smartfarming.observer;

import com.agritech.smartfarming.model.EnvironmentData;
import java.util.ArrayList;
import java.util.List;

/**
 * 环境监测器类，实现环境主题接口
 * 负责收集环境数据并通知所有观察者
 */
public class EnvironmentMonitor implements EnvironmentSubject {
    private final List<EnvironmentObserver> observers;
    private EnvironmentData environmentData;
    
    public EnvironmentMonitor() {
        this.observers = new ArrayList<>();
    }
    
    @Override
    public void registerObserver(EnvironmentObserver observer) {
        if (!observers.contains(observer)) {
            observers.add(observer);
            System.out.println("已注册观察者: " + observer.getClass().getSimpleName());
        }
    }
    
    @Override
    public void removeObserver(EnvironmentObserver observer) {
        if (observers.remove(observer)) {
            System.out.println("已移除观察者: " + observer.getClass().getSimpleName());
        }
    }
    
    @Override
    public void notifyObservers() {
        System.out.println("===== 环境数据变化，通知所有农机设备 =====");
        System.out.println(environmentData);
        System.out.println("----------------------------------------");
        
        for (EnvironmentObserver observer : observers) {
            observer.update(environmentData);
        }
    }
    
    @Override
    public void updateEnvironmentData(EnvironmentData environmentData) {
        this.environmentData = environmentData;
        System.out.println("环境监测器接收到新的环境数据");
        notifyObservers();
    }
    
    /**
     * 获取当前环境数据
     */
    public EnvironmentData getEnvironmentData() {
        return environmentData;
    }
} 